using System.Threading;
using System.Windows.Forms;
using log4net;
using SqlRunner;
using SQLRunner.uiStatus;

namespace SQLRunner.gui
{
    partial class frmMain: Form
    {

        #region Private Members

        private static readonly ILog _log = LogManager.GetLogger(typeof (frmMain).FullName);

        /// <summary>
        /// Manages the form controls so if the state changes the controls are enabled/disabled
        /// </summary>
        private readonly PropertyStateManager _formPropertyStateManager = new PropertyStateManager();

        /// <summary>
        /// Current form state
        /// </summary>
        private State _formState = State.Initial;        

        #endregion

        #region Constructors

        public frmMain()
        {
            InitializeComponent();
            InitializeMembers();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// This method is executed just at the end of the InitializeComponents method
        /// It retrieves the RunnerProperties File into a customised class: RunnerSettings
        /// </summary>
        private void InitializeMembers()
        {
            // Tree initialization: Icons
            fileTreeViewer.ImageList = new ImageList();
            fileTreeViewer.ImageList.Images.Add(IconList.Folder.ToString(), I_FOLDER);
            fileTreeViewer.ImageList.Images.Add(I_FOLDER_C);
            fileTreeViewer.ImageList.Images.Add(IconList.File.ToString(), I_FILE);
            fileTreeViewer.ImageList.Images.Add(I_FILE_C);
            fileTreeViewer.ImageList.Images.Add(I_FOLDER_OPEN);
            fileTreeViewer.ImageList.Images.Add(I_FOLDER_OPEN_C);            
        }

        


        #endregion


        private void MoveNodeDown(TreeNode node)
        {
            if (node.NextNode == null) return;
            TreeNode nextNode = node.NextNode;            
            node.Remove();
            nextNode.Parent.Nodes.Insert(nextNode.Index + 1, node);
            node.TreeView.SelectedNode = node;
            scriptController.MoveItemDown(fileTreeViewer.SelectedNode.Name);
        }

        private void MoveNodeUp(TreeNode node)
        {
            if (node.PrevNode == null) return;
            TreeNode prevNode = node.PrevNode;
            node.Remove();
            prevNode.Parent.Nodes.Insert(prevNode.Index, node);
            node.TreeView.SelectedNode = node;
            scriptController.MoveItemUp(fileTreeViewer.SelectedNode.Name);
        }
    }
}